import api from '../../api'
export default {
    namespaced: true, // 具名模块 this.$store.dispatch('home/SET_SHOP_LIST')
    // namespaced: false, // 具名模块 this.$store.dispatch('SET_SHOP_LIST')
    state: {
        cartList: [],
    },
    mutations: {
        setCartList(state, data) {
            state.cartList = data
        }
    },
    actions: {
        async GET_CART_LIST(context, data) {
            const res = await api.getCartList(data)
            if (res.success) {
                res.data = res.data.map(item => {
                    return {
                        ...item,
                        checked: false
                    }
                })
                context.commit('setCartList', res.data)
            } else {
                alert('失败了')
            }
        },
        async SET_CART_COUNT(context, { count, id }) {
            const res = await api.setCartCount({ count, id })
            if (res.success) {

            }
        },
        async DEL_CART_BY_ID(context, id) {
            // context 当前上下文
            const res = await api.delCartById(id)
            if (res.success) {
                // 手动删除数据  减少http请求 提升性能
                context.commit('setCartList', context.state.cartList.filter(item => item.id !== id))
            }
        }
    }
}